package com.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.IFinanceService;
import com.server.service.ICommonDataService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("FinanceService")
public class FinanceService implements IFinanceService {
	
	private static final String TABLE_NAME = "reward.reward";
	private static final String TABLE_VIEW = "reward.v_reward_user";
	private static final String TABLE_NAME_SOURCE = "reward.source";
	private static final String TABLE_NAME_DETAIL = "reward.reward_detail";
	private static final String VIEW_DETAIL = "reward.v_reward_detail_user";

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput, 
			Integer type){
		String whereStr = String.format("state=%s ",type);
		String sortStr = "RewardTime desc";
		return commonDataService.getListData(TABLE_VIEW, whereStr, sortStr, requestInput);
	} 
	/**
	 * 返费明细
	 * @param requestInput
	 * @param rewardId
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月24日
	 */
	@Override
	public DataVector<Map<String, Object>> getDetailList(RequestInput requestInput, 
			Integer rewardId){
		String sql = "select * from %s where Id=%s";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_VIEW,rewardId));
		if (list.size() < 1) {
			return new DataVector<Map<String,Object>>(new ArrayList<Map<String,Object>>(), 0);
		}
		Map<String, Object> map = list.get(0);
		Date start = (Date)map.get("CreateTime");
		Date end = (Date)map.get("UpdateTime");
		start.setSeconds(0);//考虑到 reward数据更新在rewarddetail之后 程序处理会出现时间误差
		end.setSeconds(59);//导致部分返费明细查询不出
		String cardId = map.get("CardID").toString();
		
		String whereStr = String.format("CreateTime<='%s' and  CreateTime>='%s' and CardID='%s'",end,start,cardId);
		String sortStr = "CreateTime desc";
		return commonDataService.getListData(VIEW_DETAIL, whereStr, sortStr, requestInput);
	} 
	 /**
	 * 审核
	 * @param operator
	 * @param ids
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月24日
	 */
	@Override
	public Boolean check(String operator,String ids,Integer state){
		String dateStr = DateUtil.getDateStr(new Date(), DateType.YYYY_MM_DD_hh_mm);
		String sql = "Update %s set CheckUser='%s', CheckTime='%s',State=%s where id in(%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME,operator,dateStr,state,ids)) > 0;
	}
	/**
	 * 返费后记录到数据库
	 * @param operator
	 * @param ids
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月24日
	 */
	@Override
	public Boolean remit(String operator,String ids){
		//1.打款
		//TODO
		
		//2.记录数据到数据库
		String dateStr = DateUtil.getDateStr(new Date(), DateType.YYYY_MM_DD_hh_mm);
		String sql = "Update %s set RewardUser='%s', RewardTime='%s',State=2 where id in(%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME,operator,dateStr,ids)) > 0;
	}
	@Override
	public Integer getCount(Integer type){
		String sql = "select Count(Id) from %s where State=%s and Valid=1";
		return jdbcTemplate.queryForObject(String.format(sql, TABLE_NAME,type), Integer.class);
	}
	@Override
	public List<Map<String, Object>> listAddSourceName(List<Map<String, Object>> list) {
		String sql = "select * from %s ";
		List<Map<String, Object>> sourceList = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_SOURCE));
		for (int i = 0; i < list.size(); i++) {
			if ( list.get(i).get("UserName")==null ) {
				for (int j = 0; j < sourceList.size(); j++) {
					if ( sourceList.get(j).get("CardID").equals(list.get(i).get("CardID")) ) {
						list.get(i).put("UserName", sourceList.get(j).get("UserName"));
						break;
					}
				}
			}
		}
		return list;
	}
	@Override
	public List<Map<String, Object>> rewardDetailAddSourceName(List<Map<String, Object>> list) {
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			if (map.get("UserName")==null) {
				map.put("UserName", map.get("UserName1"));
			}
		}
		return list;
	}
	
}
